const { resolve } = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

// css 压缩失败
// const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const path = require("path");
module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "js/bundle.js",
    path: resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      // 解析css
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader"],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      inject: "body",
    }),
    new MiniCssExtractPlugin({ filename: "css/build.css" }),
    // new OptimizeCssAssetsPlugin({}),
  ],
  // 启动devServer
  devServer: {
    //  老版本不支持contentBase
    // contentBase: resolve(__dirname, "dist"),
    static: {
      directory: path.join(__dirname, "./"),
    },
    compress: true,
    port: 3000,
    open: true,
    hot: true,
  },

  // 模式
  mode: "development",
};
